﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mighty.Common;

namespace Mighty.Brain
{
    public class OldpeopleBrain : BasicBrain
    {
        public override Betting BettingTicEvent()
        {
            Betting betting = new Betting();
            betting.isPass = true;

            int a = 0, b = 0, c = 0, d = 0, e = 0;//a=하트 b = 클로버 c= 스페이드 d= 다이아몬드 e= 조커
            int i = 0;
            for (i = 0; i < 10; i++)
            {
                HandedCard card = gameInfo.myCards[i];
                if (card.shape == Shape.Heart) a++;
                else if (card.shape == Shape.Clover) b++;
                else if (card.shape == Shape.Spade) c++;
                else if (card.shape == Shape.Diamond) d++;
                else if (card.shape == Shape.Joker) e++;
            }


            Giruda selectedShape = Giruda.Heart;
            int selectedCount = a;
            if (b > selectedCount)
            {
                selectedShape = Giruda.Clover;
                selectedCount = b;
            }
            if (c > selectedCount)
            {
                selectedShape = Giruda.Spade;
                selectedCount = c;
            }
            if (d > selectedCount)
            {
                selectedShape = Giruda.Diamond;
                selectedCount = d;
            }
            betting.giruda = selectedShape;


            betting.isPass = true;
            if (gameInfo.betting.limit > 0)
            {
                Betting previousBetting = gameInfo.betting;
                if (previousBetting.limit <15&& selectedCount >= 8)
                {
                    betting.isPass = false;
                    betting.giruda = selectedShape;
                    betting.limit = previousBetting.limit + 1;
                }

            }
            else if (selectedCount >= 7)
            {
                betting.isPass = false;
                betting.giruda = selectedShape;
                betting.limit = 13;
            }


            return betting;
        }


        public override void BettingCompleteEvent()
        {


        }


        public override HandedCard[] ChoiceCardEvent()
        {
            for (int i = 0; i < 13; i++) gameInfo.myCards[i].cmdRemove = true;
            while (true)
            {

                int cnt = 0;
                for (int i = 0; i < 13; i++) if (gameInfo.myCards[i].cmdRemove == false) cnt++;
                if (cnt == 10) break;

                int idx = -1;
                for (int i = 0; i < 13; i++)
                {
                    HandedCard hc = gameInfo.myCards[i];
                    if (hc.cmdRemove)
                    {
                        if (hc.shape == (Shape)gameInfo.betting.giruda)
                        {
                            idx = i;
                        }
                    }
                }

                if (idx == -1)
                {

                    int max = 0;
                    for (int i = 0; i < 13; i++)
                    {
                        HandedCard hc = gameInfo.myCards[i];
                        if (hc.cmdRemove)
                        {
                            if (hc.number > max)
                            {
                                max = hc.number;
                                idx = i;
                            }
                        }
                    }
                }

                if (idx != -1) gameInfo.myCards[idx].cmdRemove = false;

            }
            return gameInfo.myCards;
        }
        public override FriendPlayer ChoiceFriendEvent()
        {
            bool mightyCard = false;
            bool jokerCard = false;

            for (int i = 0; i < gameInfo.myCards.Length; i++)
            {
                if (gameInfo.myCards[i].cmdRemove != true)
                {
                    if (gameInfo.myCards[i].shape == gameInfo.mighty.shape && gameInfo.myCards[i].number == gameInfo.mighty.number)
                        mightyCard = true;
                    else if (gameInfo.myCards[i].shape == Shape.Joker)
                        jokerCard = true;
                }
            }


            gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.card;
            gameInfo.friend.cardCondition = gameInfo.mighty;

            if (mightyCard != true) gameInfo.friend.cardCondition = gameInfo.mighty;
            else if (jokerCard != true) gameInfo.friend.cardCondition = new Card(Shape.Joker, 0);
            else gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.firstwin;
            return gameInfo.friend;
        }




    }
    
}
